/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.bookmarks.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.Messages;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.common.object.Directory;
import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.object.Topic;
import etcore.webapp.grouptalk.common.service.MessageService;
import etcore.webapp.grouptalk.common.service.TopicService;
import etcore.webapp.grouptalk.util.Symbols;

/**
 * This action validates the request and executes the command of show BOOKMARKS
 * groups,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class BookmarksAction extends AbstractAction {

	private static final long serialVersionUID = 2134177798408489913L;

	// validate result
	public static final int SUCCESS = 1;

	public static final String MAX_TOPIC_SIZE = "maxTopicSize";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

		return SUCCESS;
	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {

		return PageConstants.PAGE_PATH_BOOKMARKS;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}
 
	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String directoriesKey = PageConstants.DIRECTORIES3;
		String rootGroupsKey = PageConstants.ROOTGROUPS3;
		 

		@SuppressWarnings("unchecked")
		List<Directory> directories = (List<Directory>) request
				.getAttribute(directoriesKey);

		@SuppressWarnings("unchecked")
		List<Group> rootGroups = (List<Group>) request
				.getAttribute(rootGroupsKey);
		if (directories == null && rootGroups == null)
			return;

		int msgCount = 5;
		String msgCountStr = request.getParameter("msgCount");
		if (msgCountStr != null && !Symbols.BLANK.equals(msgCountStr)) {
			msgCount = Integer.parseInt(msgCountStr);
		}
		// enable banner or not
		String hasBanner = request.getParameter(PageConstants.HASBANNER);
		if (Messages.FALSE.equals(hasBanner))
			request.setAttribute(PageConstants.HASBANNER, false);
		else
			request.setAttribute(PageConstants.HASBANNER, true);

		// enable footer or not
		String hasFooter = request.getParameter(PageConstants.HASFOOTER);
		if (Messages.FALSE.equals(hasFooter))
			request.setAttribute(PageConstants.HASFOOTER, false);
		else
			request.setAttribute(PageConstants.HASFOOTER, true);
		TopicService topicService = new TopicService();
		MessageService msgService = new MessageService();
	 
		Map<Object,Object> topicMap = new HashMap<Object, Object>();

		for (Directory directory : directories) {
			@SuppressWarnings("unchecked")
			List<Group> groups = (List<Group>) request.getAttribute(directory
					.getId() + PageConstants.GROUPS);

			for (Group group : groups) {

				List<Topic> topics = topicService.findByProperty(
						PageConstants.GID, new Integer(group.getId()));
				request.setAttribute(group.getId() + PageConstants.TOPICS,
						topics);
				for (Topic topic : topics) {
					topicMap.put(topic.getId(), topic.getName());
				}

				@SuppressWarnings("rawtypes")
				List msgs = msgService.findByGidOrderByClick(
						group.getId(), 0, msgCount);
				request.setAttribute(group.getId() + PageConstants.MSGS, msgs);
				// comment count and time
				for (int k = 0; k < msgs.size(); k++) {
					Object[] tempm = (Object[]) msgs.get(k);
					Integer tempm_id = (Integer) tempm[0];
					Long commentNum = msgService.findCommentNum(tempm_id);
				 
					request.setAttribute(tempm_id + PageConstants.COMMENT_NUM, commentNum);
			 
				}

			}
		}
		for (Group group : rootGroups) {

			List<Topic> topics = topicService.findByProperty(PageConstants.GID,
					new Integer(group.getId()));
			request.setAttribute(group.getId() + PageConstants.TOPICS, topics);

			for (Topic topic : topics) {
				topicMap.put(topic.getId(), topic.getName());
			}

			@SuppressWarnings("rawtypes")
			List msgs = msgService.findByGidOrderByClick(
					group.getId(), 0, msgCount);
			request.setAttribute(group.getId() + PageConstants.MSGS, msgs);
			for (int k = 0; k < msgs.size(); k++) {
				Object[] tempm = (Object[]) msgs.get(k);
				Integer tempm_id = (Integer) tempm[0];
				Long commentNum = msgService.findCommentNum(tempm_id);
			 
				request.setAttribute(tempm_id + PageConstants.COMMENT_NUM, commentNum);
		 
			}

		}
		request.setAttribute(PageConstants.TOPIC_MAP, topicMap);
		

	}
	public void execute2(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {

 

		int msgCount = 50;
		String msgCountStr = request.getParameter("msgCount");
		if (msgCountStr != null && !Symbols.BLANK.equals(msgCountStr)) {
			msgCount = Integer.parseInt(msgCountStr);
		}
		// enable banner or not
		String hasBanner = request.getParameter(PageConstants.HASBANNER);
		if (Messages.FALSE.equals(hasBanner))
			request.setAttribute(PageConstants.HASBANNER, false);
		else
			request.setAttribute(PageConstants.HASBANNER, true);

		// enable footer or not
		String hasFooter = request.getParameter(PageConstants.HASFOOTER);
		if (Messages.FALSE.equals(hasFooter))
			request.setAttribute(PageConstants.HASFOOTER, false);
		else
			request.setAttribute(PageConstants.HASFOOTER, true);
	 
		MessageService msgService = new MessageService();
		 
		HttpSession session = request.getSession(true);
		String lang = (String) session.getAttribute(Messages.LANG);
		@SuppressWarnings("rawtypes")
		List msgs = null;
		if (Messages.LANG_LOCAL.equals(lang)) {
			msgs = msgService.findHottestLocalMessagesByOpen(3, 0, msgCount);
		}else{
			msgs = msgService.findHottestEnglishMessagesByOpen(3, 0, msgCount);
		}

		request.setAttribute(PageConstants.MSGS, msgs);

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
